var masterDataService = require('../services/masterDataService');
var messageUtil = require('../common/utils/messageUtil');


//获取国家列表
exports.getCountryList = function (req, res) {
    logger.debug('Trying to get the country list');
    masterDataService.getCountryListService(function (err, results) {
        if (err) {
            logger.error("Failed to get the country List! " + err.message);
            res.json(messageUtil.composeFailedMessage(req,"Failed to get the country List! " + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req,null, results));
        }
    });
}

//获取某个国家下所有省
exports.getProvinceListByCountyId = function (req, res) {
    logger.debug('Trying to get the province list');
    var countryId = req.params.countryId;
    masterDataService.getProvinceListByCountyIdService(countryId, function (err, results) {
        if (err) {
            logger.error("Failed to get the province List! " + err.message);
            res.json(messageUtil.composeFailedMessage(req,"Failed to get the province List! " + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req,null, results));
        }
    });
};

//获取某个省下所有市
exports.getCityListByProvinceId = function (req, res) {
    logger.debug('Trying to get the city list');
    var provinceId = req.params.provinceId;
    masterDataService.getCityListByProvinceIdService(provinceId, function (err, results) {
        if (err) {
            logger.error("Failed to get the city List! " + err.message);
            res.json(messageUtil.composeFailedMessage(req,"Failed to get the city List! " + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req,null, results));
        }
    });
};

//获取某个市下所有县
exports.getCountyListByCityId = function (req, res) {
    logger.debug('Trying to get the city list');
    var cityId = req.params.cityId;
    masterDataService.getCountyListByCityIdService(cityId, function (err, results) {
        if (err) {
            logger.error("Failed to get the county List! " + err.message);
            res.json(messageUtil.composeFailedMessage(req,"Failed to get the county List! " + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req,null, results));
        }
    });
};

//获取某个国家、省、市、县信息
exports.getRegionById = function (req, res) {
    logger.debug('Trying to get region data');
    var regionId = req.params.regionId;
    masterDataService.getRegionByIdService(regionId, function (err, results) {
        if (err) {
            logger.error("Failed to get the region data! " + err.message);
            res.json(messageUtil.composeFailedMessage(req,"Failed to get the region data! " + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req,null, results));
        }
    });
};

exports.getRegionList = function (req, res) {
    var date = req.params.date;
    masterDataService.getRegionListService(date, function (err, results) {
        if (err) {
            logger.error("Failed to get region list! " + err.message);
            res.json(messageUtil.composeFailedMessage(req,"Failed to get the region list! " + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req,null, results));
        }
    });
}

exports.getDataDictionaryByCategory = function (req, res) {
    var category = req.params.category;
    masterDataService.getDataDictionaryByCategoryService(category, function (err, results) {
        if (err) {
            logger.error("Failed to get region list! " + err.message);
            res.json(messageUtil.composeFailedMessage(req,"Failed to get the region list! " + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req,null, results));
        }
    });
}

exports.getDataDictionary = function (req, res) {
    var date = req.params.date;
    masterDataService.getDataDictionaryService(date, function (err, results) {
        if (err) {
            logger.error("Failed to get data dictionary! " + err.message);
            res.json(messageUtil.composeFailedMessage(req,"Failed to get the data dictionary! " + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req,null, results));
        }
    });
};

exports.getUserPortrait = function (req, res) {
    var date = req.params.date;
    masterDataService.getUserPortraitService(date, function (err, results) {
        if (err) {
            logger.error("Failed to get user portrait! " + err.message);
            res.json(messageUtil.composeFailedMessage(req,"Failed to get user portrait! " + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req,null, results));
        }
    });
};

